package me.zhao.factory;

import java.util.Map;

public class HairFactory {
	/**
	 * 根据类型创建对象
	 */
	public static HairInterface getHair(String key){
		if("left".equals(key)){
			return new LeftHair();
		}else if("right".equals(key)){
			return new RightHair();
		}
		return null;
	}
	
	/**
	 * 根据类型创建对象
	 */
	public static HairInterface getHairByClass(String className){
		try {
			return (HairInterface) Class.forName(className).newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 根据配置文件类型创建对象
	 */
	public static HairInterface getHairByClassFromProps(String key){
		try {
			PropertiesUtil pu = new PropertiesUtil();
			Map typeMap = pu.getProperties();
			String className = (String) typeMap.get(key);
			return (HairInterface) Class.forName(className).newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
